package servlets;

import beans.*;
import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import service.*;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Servlet implementation class Main
 */

public class Main extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if("disagreeA".equals(action))
		{
			disagreeA(request, response);
		}
		if("agreeA".equals(action))
		{
			agreeA(request, response);
		}
		if("applyCancel".equals(action))
		{
			applyCancel(request, response);
		}
		if("updateS".equals(action))
		{
			updateSch(request, response);
		}
		if("getmzs1".equals(action))
		{
			getmzs1(request, response);
		}
		if("cancelS".equals(action))
		{
			delSch(request, response);
		}
		if("adddoctorScheduling".equals(action))
		{
			try {
				adddoctorScheduling(request, response);
			} catch (ServletException | IOException | ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if("getdoctors".equals(action))
		{
			getdoctors(request, response);
		}
		if("getmzs".equals(action))
		{
			getmzs(request, response);
		}
		if("selectdoctorScheduling".equals(action))
		{
			selectdoctorScheduling(request, response);
		}
		if("addpatient".equals(action))
		{
			addpatient(request, response);
		}
		if("pd".equals(action))
		{
			pd(request, response);
		}
	}

	private void pd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String account=request.getParameter("account");
		String password=request.getParameter("password");
		String type=request.getParameter("type");
		if("管理员".equals(type)) {
			adminService userService=new adminService();
			if(userService.login(account, password))
			{
				HttpSession session = request.getSession(); 
				session.setAttribute("account", account);
				session.setAttribute("password", password);
				response.setContentType("text/html;charset=UTF-8");
				response.sendRedirect("admin/index.jsp");
			}
			else
			{
				response.setContentType("text/html;charset=UTF-8");
				response.sendRedirect("fail.jsp");
			}
		}else if("患者".equals(type)) {
			patientService userService=new patientService();
			if(userService.login(account, password))
			{
				HttpSession session = request.getSession(); 
				session.setAttribute("patient", account);
				session.setAttribute("password", password);
				response.setContentType("text/html;charset=UTF-8");
				response.sendRedirect("patient/index.jsp");
			}
			else
			{
				response.setContentType("text/html;charset=UTF-8");
				response.sendRedirect("fail.jsp");
			}
		}else {
			doctorService userService=new doctorService();
			if(userService.login(account, password))
			{
				HttpSession session = request.getSession(); 
				session.setAttribute("id", account);
				session.setAttribute("password", password);
				response.setContentType("text/html;charset=UTF-8");
				response.sendRedirect("doctor/index.jsp");
			}
			else
			{
				response.setContentType("text/html;charset=UTF-8");
				response.sendRedirect("fail.jsp");
			}
		}
		
	}

	private void addpatient(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String account=request.getParameter("account");
		String password=request.getParameter("password");
		String name=request.getParameter("name");
		String idNumber=request.getParameter("idno");
		String sex=request.getParameter("sex");
		Date birthdate=new Date(Date.valueOf(request.getParameter("date")).getTime());
		String telNumber=request.getParameter("tel");
		patientBean patientBean=new patientBean(); 
		patientBean.setAccount(account);
		patientBean.setPassword(password);
		patientBean.setName(name);
		patientBean.setIdNumber(idNumber);
		patientBean.setBirthdate(birthdate);
		patientBean.setSex(sex);
		patientBean.setTel(telNumber);
		patientService patientService =new patientService();
		patientService.addpatient(patientBean);
		response.setContentType("text/html;charset=UTF-8");
		response.sendRedirect("main.jsp");
	}

	private void disagreeA(HttpServletRequest request, HttpServletResponse response) throws IOException {
		applicationService applicationService=new applicationService();
		int schid1=Integer.parseInt(request.getParameter("schid1"));
		String reason=request.getParameter("reason");
		String state="已驳回:"+reason;
		applicationBean befapplicationBean=new applicationBean();
		applicationBean aftapplicationBean=new applicationBean();
		befapplicationBean.setSchid(schid1);
		aftapplicationBean.setState(state);
		applicationService.updateapplication(befapplicationBean, aftapplicationBean);
		response.setContentType("text/html;charset=UTF-8");
		response.sendRedirect("admin/handleApplication.jsp");
	}

	private void agreeA(HttpServletRequest request, HttpServletResponse response) throws IOException {
		applicationService applicationService=new applicationService();
		doctorSchedulingService doctorSchedulingService=new doctorSchedulingService();
		int schid=Integer.parseInt(request.getParameter("schid"));
		String state="已取消";
		applicationBean befapplicationBean=new applicationBean();
		applicationBean aftapplicationBean=new applicationBean();
		befapplicationBean.setSchid(schid);
		aftapplicationBean.setState(state);
		applicationService.updateapplication(befapplicationBean, aftapplicationBean);
		doctorSchedulingService.deldoctorScheduling(schid);
		response.setContentType("text/html;charset=UTF-8");
		response.sendRedirect("admin/handleApplication.jsp");
	}

	private void applyCancel(HttpServletRequest request, HttpServletResponse response) throws IOException {
		applicationService applicationService=new applicationService();
		doctorSchedulingService doctorSchedulingService=new doctorSchedulingService();
		int schid=Integer.parseInt(request.getParameter("schid"));
		doctorSchedulingBean doctorSchedulingBean =doctorSchedulingService.getRecord(schid);
		String docid=doctorSchedulingBean.getDocid();
		String reason=request.getParameter("reason");
		String state="待审核";
		if(applicationService.getRecordbyschid(schid)==null) {
			applicationBean applicationBean = new applicationBean();
			applicationBean.setDocid(docid);
			applicationBean.setSchid(schid);
			applicationBean.setReason(reason);
			applicationBean.setState(state);
			applicationService.addapplication(applicationBean);
		}else {
			applicationBean befapplicationBean = new applicationBean();
			applicationBean aftapplicationBean = new applicationBean();
			befapplicationBean.setSchid(schid);
			aftapplicationBean.setState(state);
			applicationService.updateapplication(befapplicationBean, aftapplicationBean);
		}
		response.setContentType("text/html;charset=UTF-8");
		response.sendRedirect("doctor/viewScheduling.jsp");
	}

	private void updateSch(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String time=request.getParameter("time");
		String mz1=request.getParameter("mz1");
		Date date1=new Date(Date.valueOf(request.getParameter("date1")).getTime());
		int sum=Integer.parseInt(request.getParameter("sum"));
		int schid1=Integer.parseInt(request.getParameter("schid1"));
		doctorSchedulingService doctorSchedulingService=new doctorSchedulingService();
		doctorSchedulingBean aftdoctorSchedulingBean=new doctorSchedulingBean();
		doctorSchedulingBean befdoctorSchedulingBean=doctorSchedulingService.getRecord(schid1);
		aftdoctorSchedulingBean.setMzid(mz1);
		aftdoctorSchedulingBean.setDate(date1);
		aftdoctorSchedulingBean.setType(time);
		aftdoctorSchedulingBean.setSum(sum);
		doctorSchedulingService.updatedoctorScheduling(befdoctorSchedulingBean, aftdoctorSchedulingBean);
		response.setContentType("text/html;charset=UTF-8");
		response.sendRedirect("admin/doctorArrangeCancel.jsp");
	}

	private void getmzs1(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		mzService mzService = new mzService();
		doctorSchedulingService doctorSchedulingService=new doctorSchedulingService();
		List<mzBean> mzList = mzService.getmzsbyks(mzService.getRecord(doctorSchedulingService.getRecord(Integer.parseInt(request.getParameter("schid"))).getMzid()).getKsid());//ksService.getRecord1(request.getParameter("ks")).getId()
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("mzs1", mzList);
		response.getWriter().append(jsonObject.toString());
	}

	private void delSch(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int schid=Integer.parseInt(request.getParameter("schid"));
		doctorSchedulingService doctorSchedulingService=new doctorSchedulingService();
		doctorSchedulingService.deldoctorScheduling(schid);
		response.setContentType("text/html;charset=UTF-8");
		response.sendRedirect("admin/doctorArrangeCancel.jsp");
	}

	private void selectdoctorScheduling(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		String ks=request.getParameter("ks");
		String mz=request.getParameter("mz");
		String date=request.getParameter("date");
		doctorSchedulingService doctorSchedulingService=new doctorSchedulingService();
		ksService ksService=new ksService();		
		List<doctorSchedulingBean> list=new ArrayList<doctorSchedulingBean>();
		if("-1".equals(ks)&&"-1".equals(mz)) {
			if("".equals(date))
				list = doctorSchedulingService.browse();
			else {
				list = doctorSchedulingService.getdoctorSchedulingbydate(date);
			}
		}else if("-1".equals(mz)) {
			if("".equals(date))
				list = doctorSchedulingService.getdoctorSchedulingbyks(ksService.getRecord1(ks).getId());
			else {
				list = doctorSchedulingService.getdoctorSchedulingbyksdate(ksService.getRecord1(ks).getId(),date);
			}
		}else {
			if("".equals(date))
				list = doctorSchedulingService.getdoctorSchedulingbymz(mz);
			else {
				list = doctorSchedulingService.getdoctorSchedulingbymzdate(mz,date);
			}
		}
		HttpSession session = request.getSession(); 
		session.setAttribute("ss", list);
		response.setContentType("text/html;charset=UTF-8");
		response.sendRedirect("admin/doctorArrangeCancel.jsp");
	}

	private void getdoctors(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// TODO Auto-generated method stub
		doctorService doctorService = new doctorService();
		ksService ksService = new ksService();
		List<doctorBean> doctorList = doctorService.getdoctorsbyks(ksService.getRecord1(request.getParameter("ks")).getId());//ksService.getRecord1(request.getParameter("ks")).getId()
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("doctors", doctorList);
		response.getWriter().append(jsonObject.toString());
	}
	
	private void getmzs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// TODO Auto-generated method stub
		mzService mzService = new mzService();
		ksService ksService = new ksService();
		List<mzBean> mzList = mzService.getmzsbyks(ksService.getRecord1(request.getParameter("ks")).getId());//ksService.getRecord1(request.getParameter("ks")).getId()
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("mzs", mzList);
		response.getWriter().append(jsonObject.toString());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
//	public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
//	{
//		String account=request.getParameter("account");
//		String password=request.getParameter("password");
//		UserService userService=new UserService();
//
//		if(userService.login(account, password))
//		{
//			UserBean user =new UserBean();
//			user.setAccount(account);
//			HttpSession session = request.getSession(); 
//			session.setAttribute("user", user);
//			List <UserBean> list=userService.browse();
//			request.setAttribute("allUsers", list);
//			String path="/success.jsp";
//		    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//			requestDistpatcher.forward(request, response);
//		}
//		else
//		{
//			String path="fail.jsp";
//		    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//			requestDistpatcher.forward(request, response);
//		}
//	}
//	
//	public void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
//	{
//		UserService userService=new UserService();
//		//��ȡ������ת��Ϊ��Ӧ������
//		String id=request.getParameter("id");
//		int idInt=Integer.parseInt(id);
//		String account=request.getParameter("account");
//		String password=request.getParameter("password");
//		String type=request.getParameter("type");
//		int typeInt=Integer.parseInt(type);
//		String name=request.getParameter("name");
//		//�����û����󣬲����뵽���ݱ�users��
//		UserBean user=new UserBean();
//		user.setId(idInt);
//		user.setAccount(account);
//		user.setPassword(password);
//		user.setType(typeInt);
//		user.setName(name);
//		if(userService.addUser(user))
//		{
//			List <UserBean> list=userService.browse();
//			request.setAttribute("allUsers", list);
//			String path="/success.jsp";
//		    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//			requestDistpatcher.forward(request, response);			
//		}
//		else
//		{
//			String path="fail.jsp";
//		    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//			requestDistpatcher.forward(request, response);
//		}
//	}
	
	public void adddoctorScheduling(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException 
	{
		doctorSchedulingService doctorschedulingService=new doctorSchedulingService();
		String mzid=request.getParameter("mz");
		String docid=request.getParameter("doctor");
		Date date=new Date(Date.valueOf(request.getParameter("date")).getTime());
		String type=request.getParameter("time");
		int sum=Integer.parseInt(request.getParameter("sum"));
		String state="正常";
		doctorSchedulingBean doctorScheduling=new doctorSchedulingBean();
		doctorScheduling.setMzid(mzid);
		doctorScheduling.setDocid(docid);
		doctorScheduling.setDate(date);
		doctorScheduling.setType(type);
		doctorScheduling.setSum(sum);
		doctorScheduling.setState(state);
		HttpSession session = request.getSession(); 
		if(doctorschedulingService.adddoctorScheduling(doctorScheduling))
		{
			session.setAttribute("add", "1");		
		}
		else
		{
			session.setAttribute("add", "0");
		}
	    response.setContentType("text/html;charset=UTF-8");
		response.sendRedirect("admin/doctorScheduling.jsp");
	}
	
//	public void delUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
//	{
//		UserService userService=new UserService();
//		//��ȡ������ת��Ϊ��Ӧ������
//		String id=request.getParameter("id");
//		int idInt=Integer.parseInt(id);
//		String account=request.getParameter("account");
//		String password=request.getParameter("password");
//		String type=request.getParameter("type");
//		int typeInt=Integer.parseInt(type);
//		String name=request.getParameter("name");
//		//�����û����󣬲����뵽���ݱ�users��
//		UserBean user=new UserBean();
//		user.setId(idInt);
//		user.setAccount(account);
//		user.setPassword(password);
//		user.setType(typeInt);
//		user.setName(name);
//		int delResult=userService.delUser(user);
//		if(delResult==1)
//		{
//			List <UserBean> list=userService.browse();
//			request.setAttribute("allUsers", list);
//			String path="/success.jsp";
//		    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//			requestDistpatcher.forward(request, response);				
//		}
//		else if(delResult==2)
//		{
//			String path="fail.jsp";
//		    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//			requestDistpatcher.forward(request, response);
//		}
//		else
//		{
//			String path="cantdel.jsp";
//		    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//			requestDistpatcher.forward(request, response);			
//		}
//	}
//	public void toDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
//	{
//		String userid=request.getParameter("id");//ȡ�ô��ݹ������û����������ڲ�ѯ��ɾ��
//		UserBean user=null;
//		UserService userSer=new UserService();
//		user=userSer.getRecord(Integer.parseInt(userid));
//		request.setAttribute("user", user);
//		String path="/delete.jsp";
//	    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//		RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//		requestDistpatcher.forward(request, response);					
//	}
//	public void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
//	{
//		String userid=request.getParameter("id");//ȡ�ô��ݹ������û����������ڲ�ѯ��ɾ��
//		UserBean user=new UserBean();
//		UserService userSer=new UserService();
//		user=userSer.getRecord(Integer.parseInt(userid));
//		HttpSession session = request.getSession(); 
//		session.setAttribute("befuser", user);
//		String path="/edit.jsp";
//	    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//		RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//		requestDistpatcher.forward(request, response);					
//	}
//	public void updateUser(HttpServletRequest request, HttpServletResponse response) {
//		UserService userService=new UserService();
//		HttpSession session = request.getSession(); 
//		UserBean user0=(UserBean)session.getAttribute("befuser");
//		//��ȡ������ת��Ϊ��Ӧ������;
//		String id1=request.getParameter("id1");
//		int id1Int=Integer.parseInt(id1);
//		String account1=request.getParameter("account1");
//		String password1=request.getParameter("password1");
//		String type1=request.getParameter("type1");
//		int type1Int=Integer.parseInt(type1);
//		String name1=request.getParameter("name1");
//		//�����û����󣬲����뵽���ݱ�user��
//		UserBean user=new UserBean();
//		UserBean user1=new UserBean();
//		user.setId(user0.getId());
//		user.setAccount(user0.getAccount());
//		user.setPassword(user0.getPassword());
//		user.setType(user0.getType());
//		user.setName(user0.getName());
//		user1.setId(id1Int);
//		user1.setAccount(account1);
//		user1.setPassword(password1);
//		user1.setType(type1Int);
//		user1.setName(name1);
//		int updateResult=userService.updateUser(user,user1);
//		if(updateResult==1)
//		{
//			List <UserBean> list=userService.browse();
//			request.setAttribute("allUsers", list);
//			String path="/success.jsp";
//		    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//			try {
//				requestDistpatcher.forward(request, response);
//			} catch (ServletException | IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}				
//		}
//		else if(updateResult==2)
//		{
//			String path="fail.jsp";
//		    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//			try {
//				requestDistpatcher.forward(request, response);
//			} catch (ServletException | IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		else
//		{
//			String path="cantupdate.jsp";
//		    response.setContentType("text/html;charset=UTF-8");//��ֹ���벻һ�³����������룬�˾�ǰ������out���	
//			RequestDispatcher requestDistpatcher=request.getRequestDispatcher(path);
//			try {
//				requestDistpatcher.forward(request, response);
//			} catch (ServletException | IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}			
//		}
//	}
}
